package com.jy.api.util;

import com.jy.api.dao.BillRuleDao;
import com.jy.api.domain.BillRule;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by xiaohua on 2019/5/10.
 */
public class TimeUtils {


    @Autowired
    private BillRuleDao billRuleDao;


    public static int getMinTimeDifference(long begin, long end) {
        int minutes = (int) ((end - begin) / (1000 * 60));
        return minutes;
    }

    public static int getServiceMinTimeDifference(String begin, String end) {
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String fromDate = begin;
        String toDate = simpleFormat.format(new Date());
        int minutes = 0;
        try {
            long from = simpleFormat.parse(fromDate).getTime();
            long to = simpleFormat.parse(toDate).getTime();
            minutes = (int) ((to - from) / (1000 * 60));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return minutes;
    }


    public static boolean getNowTimeParity(String begin, String end) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String s2 = sdf.format(new Date());
        String[] array1 = begin.split(":");
        //开始时间
        int total1 = Integer.valueOf(array1[0]) * 3600 + Integer.valueOf(array1[1]) * 60;
        //结束时间
        String[] array2 = end.split(":");
        int total2 = Integer.valueOf(array2[0]) * 3600 + Integer.valueOf(array2[1]) * 60;
        //现在时间
        String[] array3 = s2.split(":");
        int total3 = Integer.valueOf(array3[0]) * 3600 + Integer.valueOf(array3[1]) * 60;
        if ((total1 < total3 && total3 < total2) || ((total3 < total1 && total3 < total2) && total2 < total1)) {
            return true;
        } else {
            return false;
        }
    }

    public static String formatTimeToOrderNum(String prefix) {
        String s = UuidUtil.getUuid();
        String appendStr = s.substring(s.length() - 4);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmsssss");
        return prefix + sdf.format(new Date()) + appendStr;
    }

    public static double getBillRuleAmount(BillRule billRule, int hour, int min) {
        //获取开台时间 以及现在的时间差  是分钟
        int difference = hour * 60 + min;
        double amount = 0.0;
        if (difference - billRule.getBeginOrderTime() < 0) {
            amount = billRule.getBeginOrderAmount();
        } else {
            if (billRule.getCountType() == 1) {
                amount = billRule.getOneHourAmount();
            } else {
                amount = hour * billRule.getOneHourAmount();
                if (min > 0) {
                    amount += billRule.getOneHourAmount();
                }
            }
        }
        return amount;
    }

    public static long getNowTime(String addTime, String begin, String end) {
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        s.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        Date time = null;
        try {
            time = s.parse(addTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long diff = new Date().getTime() - time.getTime();
        long days = (diff / (1000 * 60 * 60 * 24));

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String s2 = sdf.format(new Date());
        String[] array1 = begin.split(":");
        //开始时间
        int total1 = Integer.valueOf(array1[0]) * 3600 + Integer.valueOf(array1[1]) * 60;
        //结束时间
        String[] array2 = end.split(":");
        int total2 = Integer.valueOf(array2[0]) * 3600 + Integer.valueOf(array2[1]) * 60;
        //现在时间
        String[] array3 = s2.split(":");
        int total3 = Integer.valueOf(array3[0]) * 3600 + Integer.valueOf(array3[1]) * 60;
        //总时间
        int total = 24 * 60 * 60;
        long dValues = 0;
        if (days < 0 && (total1 < total3 && total3 < total2)) {
            dValues = total3 - total1;
        } else if (days < 0 && ((total3 < total1 && total3 < total2) && total2 < total1)) {
            dValues = total - total + total3;
        } else if (days > 1 && total3 > total1 && total3 > total2) {
            dValues = (days * 24 * 60 * 60) + (total2 - total1);
        } else if (days > 1 && (total1 < total3 && total3 < total2)) {
            dValues = (days * 24 * 60 * 60) + (total3 - total1);
        } else if (days > 1 && ((total3 < total1 && total3 < total2) && total2 < total1)) {
            dValues = (days * 24 * 60 * 60) + (total - total + total3);
        }
        return dValues;
    }


   /* public static long getServiceNowTime(String numBeginTime,String numEndTime, String begin, String end) {
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        s.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        Date beginTime = null;
        Date endTime = null;
        try {
            //获取台桌开始时间
            beginTime = s.parse(numBeginTime);
            //获取台桌结束
            endTime = s.parse(numEndTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long diff = new Date().getTime() - beginTime.getTime();
        long days = (diff / (1000 * 60 * 60 * 24));

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String s2 = sdf.format(new Date());
        String[] array1 = begin.split(":");
        //开始时间
        int total1 = Integer.valueOf(array1[0]) * 3600 + Integer.valueOf(array1[1]) * 60;
        //结束时间
        String[] array2 = end.split(":");
        int total2 = Integer.valueOf(array2[0]) * 3600 + Integer.valueOf(array2[1]) * 60;
        //现在时间
        String[] array3 = s2.split(":");
        int total3 = Integer.valueOf(array3[0]) * 3600 + Integer.valueOf(array3[1]) * 60;
        //总时间
        int total = 24 * 60 * 60;
        long dValues = 0;
        if (days < 0 && (total1 < total3 && total3 < total2)) {
            dValues = total3 - total1;
        } else if (days < 0 && ((total3 < total1 && total3 < total2) && total2 < total1)) {
            dValues = total - total + total3;
        } else if (days > 1 && total3 > total1 && total3 > total2) {
            dValues = (days * 24 * 60 * 60) + (total2 - total1);
        } else if (days > 1 && (total1 < total3 && total3 < total2)) {
            dValues = (days * 24 * 60 * 60) + (total3 - total1);
        } else if (days > 1 && ((total3 < total1 && total3 < total2) && total2 < total1)) {
            dValues = (days * 24 * 60 * 60) + (total - total + total3);
        }
        return dValues;
    }*/


    public static int compareTime(int start, long i, int end) {
        int s = 0;
        if ((i - start > 0) && (end - i >= 0)) {
            s = 1;
        } else if ((i - end > 0) && (60 - i > 0)) {
            s = 2;
        }
        return s;
    }

    public static int compareHourTime(int start, long i) {
        int s = 0;
        if (i - start <= 0) {
            s = 1;
        } else if (i - start > 0) {
            s = 2;
        }
        return s;
    }

    public static int compareMinTime(int start, long i) {
        int s = 0;
        if (i - start * 60 <= 0) {
            s = 1;
        } else if (i - start * 60 > 0) {
            s = 2;
        }
        return s;
    }

    public static double getOneMinAmount(double time, double amount) {
        if (amount == 0 || time == 0) {
            return 0;
        } else {
            BigDecimal big = new BigDecimal(time);
            BigDecimal small = new BigDecimal(amount);
            BigDecimal result = small.divide(big, 2, RoundingMode.HALF_UP);
            return result.doubleValue();
        }
    }


}
